package com.jeeplus.modules.work.queue.utils;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.work.queue.service.RealEstateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.jeeplus.modules.work.queue.utils.HttpUtilsBDC.SendPost;

@Component
public class RealEstateSend {
    /**
     * 业务ＩＤ
     */
    @Value("${SerialId}")
    private String SerialId;

    /**
     * 不动产在大厅所在区域的区域编号
     */
    @Value("${Sysno}")
    private String Sysno;
    /**
     * 不动产登录账号
     */
    @Value("${realEstateLoginName}")
    private String realEstateLoginName;
    /**
     * 不动产登录密码
     */
    @Value("${realEstatePassWord}")
    private String realEstatePassWord;
    /**
     * 不动产机构标识
     */
    @Value("${realEstateIdentification}")
    private String realEstateIdentification;
    /**
     * 不动产密钥
     */
    @Value("${realEstateSecretKey}")
    private String realEstateSecretKey;

    /**
     * 不动产系统排号信息上传参考号使用前需要先在前面拼接日期（格式：20220111）
     */
    @Value("${realEstateInfoRefId}")
    private String realEstateInfoRefId;

    /**
     * 不动产系统Token参考号使用前需要先在前面拼接日期（格式：20220111）
     */
    @Value("${realEstateTokenRefId}")
    private String realEstateTokenRefId;


    /**
     * 不动产Token报文编号
     */
    @Value("${realEstateTokenTrnCode}")
    private String realEstateTokenTrnCode;
    /**
     * 不动产推送排号信息报文标识
     */
    @Value("${realEstateInfoTrnCode}")
    private String realEstateInfoTrnCode;

    @Autowired
    private  RealEstateService service;

    private static final Logger logger = LoggerFactory.getLogger(RealEstateSend.class);

    public void getQuantity(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String format = df.format(new Date());
        Map<String,String> map=new HashMap<>();
        map.put("serialId",SerialId);
        map.put("sysno",Sysno);
        map.put("startTime",format);
        Map<String, Map<String, Integer>> quantity = service.getQuantity(map);
        //获取token
        String token=getToken();
        long l = System.currentTimeMillis() / 1000;
         df = new SimpleDateFormat("yyyyMMdd");
         format = df.format(new Date());
        String StringA="{"+
                "\"MsgHeader\":{"+
                "\"TrnCode\":\""+realEstateInfoTrnCode+"\","+
                "\"RefId\":\""+format+realEstateInfoRefId+"\","+
                "\"ReqRefId\":\"\","+
                "\"Token\":\""+token+"\","+
                "\"Remark\":\"\""+
                "},"+
                "\"MsgBody\":{"+
                "\"QHDM\":\"510101\","+
                "\"BLD\":\"6\","+
                "\"QD\":\"1\","+
                "\"FHSJ\":["+
                "{"+
                "\"FHLX\":\"0\","+
                "\"FHSL\":"+quantity.get("Total").get("totalGetNum")+","+
                "\"YJHSL\":"+quantity.get("Total").get("totalCallNum")+
                "},"+
                "{"+
                "\"FHLX\":\"1\","+
                "\"FHSL\":"+quantity.get("OneWindow").get("oneWindowGetNum")+","+
                "\"YJHSL\":"+quantity.get("OneWindow").get("oneWindowCallNum")+
                "}"+
                "]"+
                "}"+
                "}";
        JSONObject jsonObject = JSONObject.fromObject(StringA);
        String sign="Content="+jsonObject.toString()+"&Key="+realEstateSecretKey+"&Timestamp="+l;
        sign = DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
        String parameter="{"+
                "\"Content\":\"{"+
                "\\\"MsgHeader\\\":{"+
                "\\\"TrnCode\\\":\\\""+realEstateInfoTrnCode+"\\\","+
                "\\\"RefId\\\":\\\""+format+realEstateInfoRefId+"\\\","+
                "\\\"ReqRefId\\\":\\\"\\\","+
                "\\\"Token\\\":\\\""+token+"\\\","+
                "\\\"Remark\\\":\\\"\\\""+
                "},"+
                "\\\"MsgBody\\\":{"+
                "\\\"QHDM\\\":\\\"510101\\\","+
                "\\\"BLD\\\":\\\"6\\\","+
                "\\\"QD\\\":\\\"1\\\","+
                "\\\"FHSJ\\\":["+
                "{"+
                "\\\"FHLX\\\":\\\"0\\\","+
                "\\\"FHSL\\\":"+quantity.get("Total").get("totalGetNum")+","+
                "\\\"YJHSL\\\":"+quantity.get("Total").get("totalCallNum")+
                "},"+
                "{"+
                "\\\"FHLX\\\":\\\"1\\\","+
                "\\\"FHSL\\\":"+quantity.get("OneWindow").get("oneWindowGetNum")+","+
                "\\\"YJHSL\\\":"+quantity.get("OneWindow").get("oneWindowCallNum")+
                "}"+
                "]"+
                "}"+
                "}\","+
                "\"OrgId\":\""+realEstateIdentification+"\","+
                "\"Sign\":\""+sign+"\","+
                "\"Timestamp\":\""+l+"\""+
                "}";
        String s = SendPost(parameter);
        System.out.println(s);
        logger.error("不动产对接-推送排队叫号数量数据接口返回参数:"+s);
    }

    /**
     * 获取Token
     * @return Token
     */
    public String getToken(){
        long l = System.currentTimeMillis() / 1000;
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String format = df.format(new Date());
        String StringA="{"+
                "\"MsgHeader\":{"+
                "\"TrnCode\":\""+realEstateTokenTrnCode+"\","+
                "\"RefId\":\""+format+realEstateTokenRefId+"\","+
                "\"ReqRefId\":\"\","+
                "\"Token\":\"\","+
                "\"Remark\":\"\""+
                "},"+
                "\"MsgBody\":{"+
                "\"LoginName\":\""+realEstateLoginName+"\","+
                "\"PassWord\":\""+realEstatePassWord+"\""+
                "}}";
        String sign="Content="+StringA+"&Key="+realEstateSecretKey+"&Timestamp="+l;
        sign = DigestUtils.md5DigestAsHex(sign.getBytes()).toUpperCase();
        String parameter="{"+
                "\"Content\":\"{"+
                "\\\"MsgHeader\\\":{"+
                "\\\"TrnCode\\\":\\\""+realEstateTokenTrnCode+"\\\","+
                "\\\"RefId\\\":\\\""+format+realEstateTokenRefId+"\\\","+
                "\\\"ReqRefId\\\":\\\"\\\","+
                "\\\"Token\\\":\\\"\\\","+
                "\\\"Remark\\\":\\\"\\\""+
                "},"+
                "\\\"MsgBody\\\":{"+
                "\\\"LoginName\\\":\\\""+realEstateLoginName+"\\\","+
                "\\\"PassWord\\\":\\\""+realEstatePassWord+"\\\""+
                "}"+
                "}\","+
                "\"OrgId\":\""+realEstateIdentification+"\","+
                "\"Sign\":\""+sign+"\","+
                "\"Timestamp\":\""+l+"\""+
                "}";
        try{
            String s = SendPost(parameter);
            String content = JSONObject.fromObject(s).getString("Content");
            String token = JSONObject.fromObject(content).getJSONObject("MsgBody").getString("Token");
            logger.error("不动产对接-获取Token接口返回参数:"+s);
            return token;
        }catch (Exception e){
            return "";
        }
    }

}
